package com.eduadmin.controller;

import com.eduadmin.entity.AdminUser;
import com.eduadmin.entity.Page;
import com.eduadmin.service.IAdminLoginService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class AdminUserController {
    //构造函数自动装配

    private IAdminLoginService adminLoginService;

    @Autowired
    public AdminUserController(IAdminLoginService adminLoginService) {
        this.adminLoginService = adminLoginService;
    }

    public AdminUserController() {
    }

    // 获取所有管理员信息
    @RequestMapping(value = "getAllAdminuser")
    @ResponseBody
    public Map<String, Object> getAllAdminuser(
            @RequestParam(value = "page", required = false) String page,
            @RequestParam(value = "rows", required = false) String rows,
            HttpServletRequest request) {
        Page pageBean = new Page(Integer.parseInt(page), Integer.parseInt(rows));
        Map<String, Object> reMap = new HashMap<>();                    //查询结果map
        Map<String, Object> paraMap = new HashMap<>();              //查询参数map
        paraMap.put("offset", pageBean.getOffset());
        paraMap.put("rows", pageBean.getRows());
        List<AdminUser> adminUserListTemp = adminLoginService.getAllAdminUser(paraMap);
        List<AdminUser> adminUserList = new ArrayList<>();
        Integer onlineUserPower = (Integer) request.getSession().getAttribute("onlineUserPower");
        if ( onlineUserPower> 0) {
            for (AdminUser adminUser : adminUserListTemp) {
                if (adminUser.getPower() > 1) {
                    adminUserList.add(adminUser);
                }
            }
        } else {
            adminUserList = adminUserListTemp;
        }

        Long total = adminLoginService.getAllTotal(paraMap);
        reMap.put("rows", adminUserList);
        reMap.put("total", total);

        return reMap;
    }

    // 新增
    @ResponseBody
    @RequestMapping(value = "newAddAdminUser")
    public Map<String, String> newAddAdminUser(AdminUser adminUser,HttpServletRequest request) {
        Map<String, String> ret = new HashMap<>();
        Integer onlineUserPower = (Integer) request.getSession().getAttribute("onlineUserPower");
        if (onlineUserPower>0 && adminUser.getPower() < 2) {
            ret.put("type", "error");
            ret.put("msg", "增加失败，权限小于2！");
            return ret;
        }
        //判断是否存在
        AdminUser au = adminLoginService.getAdminByName(adminUser.getAdminLoginName());
        if (au == null) {
            adminLoginService.addAdminUser(adminUser);
            ret.put("type", "success");
            ret.put("msg", "增加成功！");
        } else {
            ret.put("type", "error");
            ret.put("msg", "增加失败，该登录名已被使用！");
        }
        return ret;
    }

    // 修改
    @ResponseBody
    @RequestMapping(value = "editAdminUser")
    public Map<String, String> editAdminUser(AdminUser adminUser ,HttpServletRequest request) {
        Map<String, String> ret = new HashMap<>();
        Integer onlineUserPower = (Integer) request.getSession().getAttribute("onlineUserPower");
        if (onlineUserPower>0 && adminUser.getPower() < 2) {
            ret.put("type", "error");
            ret.put("msg", "修改失败，权限小于2！");
            return ret;
        }
        AdminUser au = adminLoginService.getAdminByName(adminUser.getAdminLoginName());
        if (au == null || au.getAdminid() == adminUser.getAdminid()) {
            adminLoginService.editAdminUser(adminUser);
            ret.put("type", "success");
            ret.put("msg", "修改成功！");
        } else {
            ret.put("type", "error");
            ret.put("msg", "修改失败！该登录名已被使用！");
        }
        return ret;
    }

    //删除
    @RequestMapping(value = "deleteAdminUser")
    @ResponseBody
    public Map<String, String> deleteAdminUser(@RequestParam(value = "ids[]") long[] adminidArray) {
        Map<String, String> ret = new HashMap<>();
        int n = adminLoginService.deleteAdminUser(StringUtils.join(adminidArray, ','));
        if (n != 0) {
            ret.put("type", "success");
            ret.put("msg", "删除成功！");
            return ret;
        }
        ret.put("type", "error");
        ret.put("msg", "删除失败！");
        return ret;
    }


}
